package com.webssh.service.handler.terminal;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import com.webssh.service.handler.HandlerFactory;


public class TerminalHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
    	attributes.put(HandlerFactory.HANDLERTYPE,HandlerFactory.HandlerType.Terminal);
        if (request instanceof ServletServerHttpRequest) {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            attributes.put("cols", servletRequest.getParameter("cols"));
            attributes.put("rows", servletRequest.getParameter("rows"));
            attributes.put("width", servletRequest.getParameter("width"));
            attributes.put("height", servletRequest.getParameter("height"));
            if(servletRequest.getParameter("openNew") != null){
            	attributes.put("openNew", servletRequest.getParameter("openNew"));
            }
//            HttpSession session = servletRequest.getSession();
//            if(session.getAttribute(TerminalClient.SSH_SESSION_ID) != null){
//            	attributes.put(TerminalClient.SSH_SESSION_ID,session.getAttribute(TerminalClient.SSH_SESSION_ID));
//            }
        }
        return super.beforeHandshake(request,response,wsHandler,attributes);
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        super.afterHandshake(request,response,wsHandler,exception);
    }

}
